package com.jjw.route.service.impl;

import com.jjw.route.algorithm.RandomArticalAlogorithm;
import com.jjw.route.common.LoginEnum;
import com.jjw.route.dao.PortraitDao;
import com.jjw.route.entity.PortraitEntity;
import com.jjw.route.to.LoginTo;
import com.jjw.route.utils.R;
import com.jjw.route.vo.PointVo;
import com.jjw.route.vo.SimVo;
import org.springframework.aop.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jjw.route.utils.PageUtils;
import com.jjw.route.utils.Query;

import com.jjw.route.dao.UserDao;
import com.jjw.route.entity.UserEntity;
import com.jjw.route.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    PortraitDao portraitDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }
    @Override
    public R register(LoginTo loginTo) {
        QueryWrapper<UserEntity> userEntityQueryWrapper = new QueryWrapper<UserEntity>().eq("name",loginTo.getName());
        List<UserEntity> userEntities = userDao.selectList(userEntityQueryWrapper);
        if (!userEntities.isEmpty()) {
            return R.error(LoginEnum.USER_EXIST_EXCEPTION.getCode(), LoginEnum.USER_EXIST_EXCEPTION.getMessage());
        }
        UserEntity entity = new UserEntity(loginTo.getName(),loginTo.getPassword());
        int insert = userDao.insert(entity);
        if (insert != 0) {
            QueryWrapper<UserEntity> user = new QueryWrapper<UserEntity>().eq("name",loginTo.getName());
            UserEntity userEntity = userDao.selectOne(user);
            portraitDao.insert(new PortraitEntity(userEntity.getId()));
            return R.ok();
        }
        return R.error(LoginEnum.UNKNOW_EXCEPTION.getCode(),LoginEnum.UNKNOW_EXCEPTION.getMessage());
    }
    @Override
    public R login(LoginTo loginTo, HttpSession session) {
        QueryWrapper<UserEntity> userEntityQueryWrapper = new QueryWrapper<UserEntity>().eq("name", loginTo.getName());
        UserEntity userEntity = userDao.selectOne(userEntityQueryWrapper);
        if (userEntity == null)
            return R.error(LoginEnum.USER_NOT_EXIST_EXCEPTION.getCode(),LoginEnum.USER_NOT_EXIST_EXCEPTION.getMessage());
        if (!userEntity.getPassword().equals(loginTo.getPassword()))
            return R.error(LoginEnum.LOGIN_PASSWORD_EXCEPTION.getCode(),LoginEnum.LOGIN_PASSWORD_EXCEPTION.getMessage());
        session.setAttribute("uid",userEntity.getId());
        return R.ok().put("userName",userEntity.getName()).put("userId",userEntity.getId());
    }

    @Override
    public R saveData(String[] arr) {
        Random random = new Random();
        for (String userName : arr) {
            int i = random.nextInt(RandomArticalAlogorithm.url.length - 1);
            UserEntity user = new UserEntity();
            user.setName(userName);
            user.setPassword("12345678");
            user.setImg(RandomArticalAlogorithm.url[i]);
            userDao.insert(user);
            QueryWrapper<UserEntity> uu = new QueryWrapper<UserEntity>().eq("name",userName);
            UserEntity userEntity = userDao.selectOne(uu);
            portraitDao.insert(new PortraitEntity(userEntity.getId()));
        }
        return R.ok();
    }

    @Override
    public UserEntity selectByName(String userName) {
        UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("name", userName));
        return userEntity;
    }

    @Override
    public List<UserEntity> selectByIds(List<Integer> uids) {
        List<UserEntity> userEntities = userDao.selectList(new QueryWrapper<UserEntity>().in("id", uids));
        return userEntities;
    }

    @Override
    public UserEntity selectById(Integer uid) {
        return userDao.selectOne(new QueryWrapper<UserEntity>().eq("id",uid));
    }

    @Override
    public R getSim() {
        PointVo p1 = new PointVo(1, "天安门");
        PointVo p2 = new PointVo(2, "天坛公园");
        PointVo p3 = new PointVo(3,"故宫博物院");
        PointVo p4 = new PointVo(4,"北京大学");
        PointVo p5 = new PointVo(5,"中山公园");
        PointVo p6 =new PointVo(6,"清华大学");
        PointVo p7 =new PointVo(7,"北京自然博物馆");
        PointVo p8 = new PointVo(8,"世界公园");
        List<Integer> userNames = new ArrayList<>();
        userNames.add(333);
        userNames.add(318);
        List<UserEntity> userEntities = userDao.selectList(new QueryWrapper<UserEntity>().in("id", userNames));

        List<SimVo> simVos = new ArrayList<>();
        SimVo simVo1 = new SimVo();
        simVo1.setId(1);
        simVo1.setName(userEntities.get(0).getName());
        simVo1.setImg(userEntities.get(0).getImg());
        List<PointVo> route1 = new ArrayList<>();
        route1.add(p1);
        route1.add(p2);
        route1.add(p3);
        route1.add(p4);
        route1.add(p5);
        simVo1.setRoute(route1);

        SimVo simVo2 = new SimVo();
        simVo2.setId(2);
        simVo2.setName(userEntities.get(1).getName());
        simVo2.setImg(userEntities.get(1).getImg());
        List<PointVo> route2 = new ArrayList<>();
        route2.add(p5);
        route2.add(p2);
        route2.add(p3);
        route2.add(p7);
        route2.add(p8);
        simVo2.setRoute(route2);

        simVos.add(simVo1);
        simVos.add(simVo2);
        return R.ok().put("data",simVos);
    }

}